<?php
/**
 * EmailCommand class file.
 *
 * @author hwh4729 <hwh4729@gmail.com>
 * @link http://www.yiiframework.com/
 * @copyright Copyright &copy; 2008-2010 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 * @version $Id: EmailCommand.php 1678 2010-01-07 21:02:00Z qiang.xue $
 */

/**
 * HelpCommand displays help information for commands under yiic shell.
 *
 * @author hwh4729 <hwh4729@gmail.com>
 * @version $Id: EmailCommand.php 1678 2010-01-07 21:02:00Z qiang.xue $
 * @package system.cli.commands.shell
 * @since 1.0
 */
	define('NCMAIL_DOMAIN','163.com');
	define('NCMAIL_EMAIL','hwh4729@163.com');
	define('NCMAIL_PASS','870805');
	define('NCMAIL_USERNAME','hwh4729');
	
class EmailCommand extends CConsoleCommand{
	private $_schema;
	private $_tables;
	private $_classes;
	public function run($args){
		$runner = $this->getCommandRunner();
		$commands = $runner->commands;
		if(isset($args[0]))
			$name = strtolower($args[0]);
		if(!isset($args[0])){
			echo "Error:model class name is required.\n";
			echo $this->getHelp();
			return;
		}
		$modelClass = Yii::import($args[0],true);
		$model = new $modelClass();
		$emailList = $model->getEmailList();
		for($i = 0,$num = count($emailList);$i<$num;$i++){

			//发送邮件
			$PHPMailer = Yii::import('PHPMailer',true);
			$mail = new $PHPMailer(); 
			$mail->IsSMTP(); // set mailer to use SMTP 
			$mail->Host = "smtp.".NCMAIL_DOMAIN.";"; // specify main and backup server 
			$mail->SMTPAuth = true; // turn on SMTP authentication 
			$mail->Username = NCMAIL_USERNAME; // SMTP username 
			$mail->Password = NCMAIL_PASS; // SMTP password 
			$mail->From = NCMAIL_EMAIL; 
			$mail->FromName = NCMAIL_USERNAME; 
			$mail->AddAddress($emailList[$i]->email); // name is optional 
			$mail->AddReplyTo(NCMAIL_EMAIL, "Information");
			$mail->WordWrap = 50; // set word wrap to 50 characters 
			$mail->IsHTML(true); // set email format to HTML
			$mail->Subject = $emailList[$i]->subject; 
			$mail->Body = $emailList[$i]->content;
			$mail->AltBody = "这是一封来自NewsBag的邮件";
			$tag = $mail->Send();
			
			if($tag){
				echo 'send succuss!';
				$model->updateEmailFlag($emailList[$i],'sended');
			} else{
				echo $mail->ErrorInfo;
				$model->updateEmailFlag($emailList[$i],'error');
			}
		}
	}

	public function getHelp(){
		return <<<EOD
	USAGE
		Email [EmailQueue];
EOD;
	}
}
?>